// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var {carArr,userAddress} = event;
  var posttime = Date.now();
  var orderNum =String(posttime)+String(Math.random()).substr(3,8)
 await db.collection("xiangcun_order_add").add({
    data:{
      openid,
      carArr,
      userAddress,
      posttime,
      orderNum, 
      state:0
    }
  })
  var newCarArr = carArr.map(item=>{
    return item._id
  })
  await db.collection("xiangcun_cars").where({
    _id:_.in(newCarArr),
    openid
  }).remove()

  return {msg:"添加成功",code:1111,orderNum:orderNum}


}